<?php
// 表字段可选的字段类型
use app\common\middleware\CreateCrudMiddleware;
use app\common\middleware\CreateRelationMiddleware;

$tableFieldTypes = ["int", "tinyint", "integer", "float", "decimal", "char", "varchar", "blob", "text", "point"];
// 表字段可选的验证规则
$tableFieldCheckOptions = [
    ['label' => '必填', 'value' => 'require', 'message' => '缺少必要参数{$item.field}'],
    ['label' => '数字', 'value' => 'number', 'message' => '{$item.field}字段类型为数字'],
    ['label' => '整数', 'value' => 'integer', 'message' => '{$item.field}为整数'],
    ['label' => '布尔', 'value' => 'boolean', 'message' => '{$item.field}为布尔值'],
];
// 主表默认字段
$tableDefaultRows = [
    [
        'field' => 'id',
        'desc' => '唯一id',
        'type' => 'int',
        'length' => 11,
        'default' => '',
        'not_null' => true,
        'main_key' => true,
        'incremental' => true,
        'query' => false,
        'list' => true,
        'detail' => true,
        'add' => false,
        'edit' => true
    ],
    [
        'field' => 'create_at',
        'desc' => '创建时间',
        'type' => 'datetime',
        'length' => 0,
        'default' => '',
        'not_null' => false,
        'main_key' => false,
        'incremental' => false,
        'list' => true,
        'detail' => true
    ],
    [
        'field' => 'update_at',
        'desc' => '更新时间',
        'type' => 'datetime',
        'length' => 0,
        'default' => '',
        'not_null' => false,
        'main_key' => false,
        'incremental' => false,
        'list' => true,
        'detail' => true
    ]
];

//默认字段
$tableDefault = [
    'type' => 'varchar',
    'length' => 255,
    'list' => true,
    'detail' => true,
    'add' => true,
    'edit' => true,
];

// crud的表配置自定义列
$crudTableColumns = [
    [
        'title' => '验证',
        'field' => 'check',
        'type' => 'select',
        'width' => 180,
        'props' => [
            'placeholder' => '请输入',
            'mode' => 'multiple',
            'maxTagCount' => 1,
            'options' => $tableFieldCheckOptions
        ],
    ],
    [
        'title' => '查询',
        'field' => 'query',
        'type' => 'checkbox',
        'width' => 60
    ],
    [
        'title' => '列表',
        'field' => 'list',
        'type' => 'checkbox',
        'width' => 60
    ],
    [
        'title' => '明细',
        'field' => 'detail',
        'type' => 'checkbox',
        'width' => 60
    ],
    [
        'title' => '新增',
        'field' => 'add',
        'type' => 'checkbox',
        'width' => 60
    ],
    [
        'title' => '编辑',
        'field' => 'edit',
        'type' => 'checkbox',
        'width' => 60
    ]
];
// 模型名规则
$modelNameRules = [
    ['pattern' => '^[A-Z]{1}([a-zA-Z0-9]|[._]){2,19}$', 'message' => '模型文件名错误，请输入大写字母开头的字母+数字，长度2-19的组合']
];
// 表名规则
$tableNameRules = [
    ['pattern' => '^[a-z]{1}([a-z0-9]|[_]){2,19}$', 'message' => '表名错误，请输入小写字母开头的字母+数字/下划线，长度2-19的组合']
];

return [
    // 文档标题
    'title' => 'API接口文档',
    // 文档描述
    'desc' => '',
    // 默认请求类型
    'default_method' => 'GET',
    // 允许跨域访问
    'allowCrossDomain' => false,
    // 设置可选版本
    'apps' => [
        ['title' => '后台管理', 'path' => 'app\manage\controller', 'folder' => 'manage'],
        ['title' => '前台页面', 'path' => 'app\index\controller', 'folder' => 'index'],
    ],
    // 自动生成url规则
    'auto_url' => [
        // 字母规则
        'letter_rule' => "lcfirst",
        // 多级路由分隔符
        'multistage_route_separator' => "."
    ],
    // 指定公共注释定义的文件地址
    'definitions' => "app\common\controller\Definitions",
    // 缓存配置
    'cache' => [
        // 是否开启缓存
        'enable' => false,
    ],
    // 权限认证配置
    'auth' => [
        // 是否启用密码验证
        'enable' => true,
        // 全局访问密码
        'password' => "123456",
        // 密码加密盐
        'secret_key' => "apidoc#hg_code",
        // 有效期
        'expire' => 3600
    ],
    // 统一的请求Header
    'headers' => [],
    // 统一的请求参数Parameters
    'parameters' => [],
    // 统一的请求响应体
    'responses' => [
        ['name' => 'status', 'desc' => '状态码', 'type' => 'int'],
        ['name' => 'message', 'desc' => '业务信息', 'type' => 'string'],
        ['name' => 'data', 'desc' => '业务数据', 'main' => true, 'type' => 'object'],
    ],
    // md文档
    'docs' => [],
    'generator' => [
        [
            'title' => '创建Crud',
            'enable' => true,
            'middleware' => [
                CreateCrudMiddleware::class
            ],
            'form' => [
                'colspan' => 3,
                'items' => [
                    [
                        'title' => '控制器标题',
                        'field' => 'controller_title',
                        'type' => 'input'
                    ],
                ]
            ],
            'files' => [
                [
                    'path' => 'app\${app[0].folder}\controller\${app[1].folder}',
                    'namespace' => 'app\${app[0].folder}\controller\${app[1].folder}',
                    'template' => 'template\crud\controller.tpl',
                    'name' => 'controller',
                    'rules' => [
                        ['required' => true, 'message' => '请输入控制器文件名'],
                        ['pattern' => '^[A-Z]{1}([a-zA-Z0-9]|[._]){2,19}$', 'message' => '请输入正确的目录名'],
                    ]
                ],
                [
                    'name' => 'service',
                    'path' => 'app\${app[0].folder}\services',
                    'template' => 'template\crud\service.tpl',
                ],
                [
                    'name' => 'validate',
                    'path' => 'app\${app[0].folder}\validate',
                    'template' => 'template\crud\validate.tpl',
                ],
                [
                    'name' => 'route',
                    'path' => 'app\${app[0].folder}\route\${app[0].folder}.php',
                    'template' => 'template\crud\route.tpl',
                ],
            ],
            'table' => [
                'field_types' => $tableFieldTypes,
                'items' => [
                    [
                        'title' => '数据表',
                        'namespace' => 'app\model',
                        'path' => "app\model",
                        'template' => "template\crud\model.tpl",
                        'model_rules' => $modelNameRules,
                        'table_rules' => $tableNameRules,
                        'columns' => $crudTableColumns,
                        'default_fields' => $tableDefaultRows,
                        'default_values' => $tableDefault,
                    ],
                ]
            ]
        ],
        [
            'title' => '创建一对多关联Crud',
            'middleware' => [
                CreateRelationMiddleware::class
            ],
            'form' => [
                'colspan' => 4,
                'items' => [
                    [
                        'title' => '控制器标题',
                        'field' => 'controller_title',
                        'type' => 'input',
                    ],
                    [
                        'title' => '关联字段',
                        'field' => 'relation_field',
                        'type' => 'input',
                        'props' => [
                            'placeholder' => '副表关联主表的字段，如:info_id'
                        ],
                        'rules' => [
                            // 必填的验证
                            ['required' => true, 'message' => '请输入控制器文件名'],
                        ]
                    ],
                ]
            ],
            'files' => [
                [
                    'path' => 'app\${app[0].folder}\controller\${app[1].folder}',
                    'namespace' => 'app\${app[0].folder}\controller\${app[1].folder}',
                    'template' => 'template\relation\controller.tpl',
                    'name' => 'controller',
                    'rules' => [
                        ['required' => true, 'message' => '请输入控制器文件名'],
                        ['pattern' => '^[A-Z]{1}([a-zA-Z0-9]|[._]){2,19}$', 'message' => '请输入正确的目录名'],
                    ]
                ],
                [
                    'name' => 'service',
                    'path' => 'app\${app[0].folder}\services',
                    'template' => 'template\relation\service.tpl',
                ],
                [
                    'name' => 'validate',
                    'path' => 'app\${app[0].folder}\validate',
                    'template' => 'template\relation\validate.tpl',
                ],
                [
                    'name' => 'route',
                    'path' => 'app\${app[0].folder}\route\${app[0].folder}.php',
                    'template' => 'template\relation\route.tpl',
                ],
            ],
            'table' => [
                'field_types' => $tableFieldTypes,
                'items' => [
                    [
                        'title' => '主表',
                        'desc' => '表注释必填，以便生成关联接口的名称，如：文章',
                        'namespace' => 'app\model',
                        'path' => "app\model",
                        'template' => "template\\relation\model.tpl",
                        'model_rules' => $modelNameRules,
                        'table_rules' => $tableNameRules,
                        'columns' => $crudTableColumns,
                        'default_fields' => $tableDefaultRows,
                        'default_values' => $tableDefault,
                    ],
                    [
                        'title' => '副表',
                        'desc' => '表注释必填，以便生成关联接口的名称，如：评论。必须存在与上方所填关联字段相同的字段，如:info_id',
                        'namespace' => 'app\model',
                        'path' => "app\model",
                        'template' => "template\\relation\model_sub.tpl",
                        'model_rules' => $modelNameRules,
                        'table_rules' => $tableNameRules,
                        'columns' => [
                            [
                                'title' => '新增',
                                'field' => 'add',
                                'type' => 'checkbox',
                                'width' => 60
                            ],
                            [
                                'title' => '编辑',
                                'field' => 'edit',
                                'type' => 'checkbox',
                                'width' => 60
                            ]
                        ],
                        'default_fields' => $tableDefaultRows,
                        'default_values' => $tableDefault,
                    ]
                ]
            ]
        ]
    ]
];
